package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.view.addons.TableColumnAdjuster;
import vcp.client.forms.ApproveCostForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.entity.SiteCostUpdates;
import vcp.enums.PopUpMessageType;
import vcp.enums.SiteEnum;
import vcp.enums.SiteEnum.SiteCostsUpdateStatus;
/**
 * <p> user interface approve costs screen class </p>
 * @author VCP team
 *
 */
public class UIApproveCostsView extends UIAbstractView
{

	/**
	 * 
	 */
	private static final long serialVersionUID = - 2571301596435395031L;

	private JButton btnReject;
	private JButton btnApprove;

	private JTable table;

	private String[] tableHeaders;

	private IFormSubmitionListener <ApproveCostForm> formSubmitionListener;
	/**
	 * setter
	 * @param formSubmitionListener
	 */
	public void setFormSubmitionListener ( IFormSubmitionListener <ApproveCostForm> formSubmitionListener )
	{
		this.formSubmitionListener = formSubmitionListener;
	}
	/**
	 * constructor
	 */
	public UIApproveCostsView ()
	{
		super();
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * loads all pending approval site costs update requests
	 * @param pendingSiteCostsUpdates
	 */
	public void loadPendingSiteCostsUpdates ( List <SiteCostUpdates> pendingSiteCostsUpdates )
	{
		DefaultTableModel dtm = new DefaultTableModel();
		for ( String header : this.tableHeaders )
		{
			dtm.addColumn(header);
		}

		for ( SiteCostUpdates siteCostUpdates : pendingSiteCostsUpdates )
		{
			dtm.addRow(new Object[]
					{ siteCostUpdates.getSiteEnum().toString()
					, new Float(siteCostUpdates.getSingleUsePricePerHour())
					, new Float(siteCostUpdates.getOrderAheadPricePerHour())
					, new Float(siteCostUpdates.getCasualPriceSingleCar())
					, new Float(siteCostUpdates.getFullPriceSingleCar()) });
		}

		this.setControlButtonsEnables(pendingSiteCostsUpdates.size() > 0);
		new TableColumnAdjuster(this.table).adjustColumns();
		this.table.setModel(dtm);
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * Enables control buttons
	 * @param enabled
	 */
	private void setControlButtonsEnables ( boolean enabled )
	{
		this.btnApprove.setEnabled(enabled);
		this.btnReject.setEnabled(enabled);
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public void clearView ()
	{
	}

	@Override
	protected void setupLayout ()
	{
		this.tableHeaders = new String[]
				{ "Site Name"
				, "Single Use Per Hour"
				, "Order Ahead Per Hour"
				, "Casual Use Per Month"
				, "Full Use Per Month" };

		this.setPreferredSize(new Dimension(908, 704));

		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 60));
		panel.setLayout(new GridLayout(0, 6, 0, 0));
		this.add(panel, BorderLayout.SOUTH);

		JPanel panel_2 = new JPanel();
		panel.add(panel_2);

		this.btnReject = new JButton(Common.BTN_REJECT);
		this.btnReject.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnReject.setName(Common.BTN_REJECT);
		panel.add(this.btnReject);

		JPanel panel_1 = new JPanel();
		panel.add(panel_1);

		JPanel panel_3 = new JPanel();
		panel.add(panel_3);

		this.btnApprove = new JButton(Common.BTN_APPROVE);
		this.btnApprove.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnApprove.setName(Common.BTN_APPROVE);
		this.btnApprove.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		panel.add(this.btnApprove);

		JPanel panel_5 = new JPanel();
		panel_5.setPreferredSize(new Dimension(10, 60));
		this.add(panel_5, BorderLayout.NORTH);
		panel_5.setLayout(new GridLayout(0, 4, 0, 0));

		JPanel panel_4 = new JPanel();
		panel_5.add(panel_4);

		JPanel panel_24 = new JPanel();
		panel_5.add(panel_24);

		JPanel panel_22 = new JPanel();
		panel_5.add(panel_22);

		JPanel panel_23 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_23.getLayout();
		flowLayout_4.setVgap(15);
		flowLayout_4.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_23);

		JPanel panel_6 = new JPanel();
		panel_6.setPreferredSize(new Dimension(120, 10));
		this.add(panel_6, BorderLayout.WEST);

		JPanel panel_7 = new JPanel();
		panel_7.setPreferredSize(new Dimension(120, 10));
		this.add(panel_7, BorderLayout.EAST);

		JPanel panel_8 = new JPanel();
		this.add(panel_8, BorderLayout.CENTER);
		panel_8.setLayout(new BorderLayout(0, 0));

		JPanel panel_9 = new JPanel();
		panel_9.setPreferredSize(new Dimension(10, 267));
		panel_8.add(panel_9, BorderLayout.SOUTH);

		JScrollPane scrollPane = new JScrollPane();
		panel_8.add(scrollPane, BorderLayout.CENTER);

		final DefaultTableCellRenderer renderRight = new DefaultTableCellRenderer();

		renderRight.setHorizontalAlignment(SwingConstants.CENTER);

		this.table = new JTable()
		{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable ( int row, int column )
			{
				return false;
			}

			@Override
			public void setRowHeight ( int rowHeight )
			{
				super.setRowHeight(45);
			}

			@Override
			public TableCellRenderer getCellRenderer ( int arg0, int arg1 )
			{
				return renderRight;
			}
		};

		this.table.setGridColor(Color.BLACK);
		this.table.setShowHorizontalLines(true);
		this.table.setShowVerticalLines(true);
		this.table.setBorder(new EtchedBorder(EtchedBorder.RAISED));
		this.table.setShowGrid(true);
		this.table.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		scrollPane.setViewportView(this.table);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnApprove.addActionListener(this);
		this.btnReject.addActionListener(this);
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 40, 40);
		if ( icon != null )
		{
			this.btnApprove.setIcon(icon);
			this.btnApprove.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CANCEL, 40, 40);
		if ( icon != null )
		{
			this.btnReject.setIcon(icon);
			this.btnReject.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		if ( this.controllerDelegate != null )
		{
			JButton approveCosts = (JButton) actionEvent.getSource();
			SiteCostsUpdateStatus status = approveCosts.getName() == Common.BTN_APPROVE
					? SiteCostsUpdateStatus.APPROVED
							: SiteCostsUpdateStatus.REJECTED;

			ApproveCostForm form = new ApproveCostForm();
			for ( int row : this.table.getSelectedRows() )
			{
				String siteName = (String) this.table.getValueAt(row, 0);
				SiteEnum siteEnum = SiteEnum.convert(siteName);
				form.addForm(siteEnum, status);
			}

			if ( form.hasForms() )
			{
				this.formSubmitionListener.onFormSubmitted(new FormSubmitionEvent <>(this, form));
			}
			else
			{
				this.controllerDelegate.showPopUpMessage(UserNotification.PLEASE_SELECTED_A_ROW, PopUpMessageType.WARNING);
			}
		}
	}
}
